#!/bin/bash

yum_conf="${CPATH}/repo_conf/obs-x86_64.conf"

function kiwi_init()
{
    if which kiwi &> /dev/null; then
        echo "kiwi has been ok"
    else
        yum clean all -c $yum_conf
        yum install -y kiwi -c $yum_conf
    fi
    umask_value=`umask`
    if [ "x${umask_value}" != "x0022" ]; then
        umask 0022
    fi
    if [ ! -d /var/run/screen/S-root ]; then
	mkdir -p /var/run/screen/S-root
    fi
}

function make_docker_image()
{
    chroot_run "cd /home; bash make_version.sh make_docker_image_inchroot"
}

function make_docker_image_inchroot()
{
    #docker_init
    kiwi_init
    get_version
    release_dir=`get_release_dir`

    version_time=${release_dir#/srv/www/htdocs/}
    version_time=${version_time##*/}

    repo_dir="${WORK_DIR}repository"
    img_dir="${WORK_DIR}image"
    cfg_dir="${WORK_DIR}config"

    if [ -d $img_dir ]; then
        rm -rf $img_dir
    fi

    mkdir -p $img_dir

    if [ -d $repo_dir ]; then
        rm -rf $repo_dir
    fi

    mkdir -p $repo_dir

    if [ -d $cfg_dir ]; then
        rm -rf $cfg_dir
    fi

    mkdir -p $cfg_dir

    # download release file from server
    #srv_release_path="$HTTP_DIR/$PRE_VERSION/$VERSION/release"
    #sshscp_from "$srv_release_path" "$repo_dir"
    #if [ $? -ne 0 ]; then
    #    log_error "Failed on sshscp_from $srv_release_path $repo_dir" &> /dev/null
    #fi

    #srv_iso_path=`cat $repo_dir/release`
    #srv_iso_path=${srv_iso_path#*$IP}
    #srv_iso_path="$HTTP_DIR/$srv_iso_path"

    RELEASE_DIR="$release_dir/$SUB_RELEASE_DIR/DockerStack/" 

    SSH_CMD="mkdir -p $RELEASE_DIR"
    sshcmd "$SSH_CMD"

    # download daily release iso
    #sshscp_from "$srv_iso_path" "$repo_dir"
    #if [ $? -ne 0 ]; then
    #    log_error "Failed on sshscp_from $srv_iso_path $repo_dir" &> /dev/null
    #fi

    #srv_docker_path=${RELEASE_DIR}
    #iso_name=${srv_iso_path##*/}
    #iso_name="$repo_dir/$iso_name"

    # To workaround one kiwi problem, otherwise, kiwi will copy passwd and group file
    # from host to baseimage's rootfs.
    # Copy passwd(from setup package) file to /var/adm/fillup-templates/passwd.aaa_base
    # Copy group(from setup package) file to /var/adm/fillup-templates/group.aaa_base
    mkdir -p /var/adm/fillup-templates/
    cp passwd /var/adm/fillup-templates/passwd.aaa_base
    cp group /var/adm/fillup-templates/group.aaa_base

    # build
    sed -i "s#IMAGE_NAME#$version_time#" config/config.xml
    for repo_url in `echo ${OBS_PROJECTS} | xargs`
    do
        sed -i "/obs_repo_here/a <repository type=\"rpm-md\"><source path=\"${repo_url}\" \/></repository>" config/config.xml
    done
    cp -a config/config.xml $cfg_dir/config.xml
	cp config/images.sh $cfg_dir

    chmod 700 /var/run/screen/S-root
    #Removing yum repos in kiwi build
    if [ -d /var/cache/kiwi/yum ];then
        rm -rf /var/cache/kiwi/yum
    fi
    kiwi -build $cfg_dir -d $img_dir
    if [ $? -ne 0 ];then
        log_error "Failed on kiwi build docker image" &> /dev/null
    fi

    # upload image
    docker_img_path_tmp="`ls $img_dir/*.tar.xz`"
    docker_img_path="$img_dir/$version-docker.x86_64.tar.xz"
    mv $docker_img_path_tmp $docker_img_path
	
    cd $img_dir
    sha256sum ${docker_img_path##*/} > ${docker_img_path##*/}${SHA256SUM}
    cd - > /dev/null
    sshscp "${docker_img_path} ${docker_img_path}${SHA256SUM}" "$RELEASE_DIR"
    if [ $? -ne 0 ]; then
        log_error "Failed upload docker image"
    fi

    log_info "Release $docker_img_path to $IP:$RELEASE_DIR"
}
